layui.use(['table', 'admin', 'ax', 'func', 'util', 'form', 'laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;
    var util = layui.util;
    var form = layui.form;
    var laydate = layui.laydate;

    laydate.render({
        elem: '#startDate'
    });
    laydate.render({
        elem: '#endDate'
    });


    /**
     * 初始门店
     */
    function initShop(){
        $("#shopName").empty();
        $("#shopName").append("<option value=''>请选择门店</option>");
        var ajax = new $ax(Feng.ctxPath + "/mdzx/ghsList");
        var result = ajax.start();
        $.each(result.data, function (index, val) {
            if(val.btyBrandIdView!=undefined){
                //查询用的门店
                $("#shopName").append("<option value='" + val.btyBrandIdView + "'>" + val.btyBrandIdView + "</option>");
            }
        });
        form.render('select');
    }
    initShop();

    // 初始供货商
    function initGhs(){
        $("#userId").empty();
        $("#userId").append("<option value=''>请选择供货商</option>");
        var ajax = new $ax(Feng.ctxPath + "/btyZxBaoxiaodan/ghsList");
        var result = ajax.start();
        $.each(result, function (index, val) {
            $("#userId").append("<option value='" + val.userId + "'>" + val.userName + "</option>");
        });
        form.render('select');
    }
    initGhs();

    /**
     * 管理
     */
    var BtyZxBaoxiaodanAudit = {
        tableId: "btyZxBaoxiaodanAuditTable"
    };

    /**
     * 初始化表格的列
     */
    BtyZxBaoxiaodanAudit.initColumn = function () {
         return [[
            {field: 'id', hide: true, title: '主键'},
            {field: 'shopName', title: '店名', width: 200},
            {field: 'userName', title: '报销人', width: 100},
             {field: 'payee', title: '收款人名称', width: 100},
             {field: 'payeeCard', title: '收款人卡号', width: 200},
            {field: 'reimbursementAmount', sort: true, title: '报销金额'},
            {field: 'productRemark', title: '货品统称备注'},
            {field: 'applyDate', title: '申请日期', templet: function (dd) {
                  if (dd.applyDate) {
                      return util.toDateString(dd.applyDate, "yyyy-MM-dd");
                  } else {
                    return "";
                  }
            }},
            {field: 'auditTime', title: '通过/驳回时间', templet: function (dd) {
                if (dd.auditTime == '1970-01-01 08:00:00') {
                   return "";
                }
                return dd.auditTime;
            }},
            {field: 'reason', title: '驳回原因'},
            // 状态 1.待提交，2.工程待审核，3.工程驳回，4.财务待审核，5.财务驳回，6.待打款，7.已打款；
            {field: 'status', title: '状态', templet: function (d) {
                 if (d.status == 1) {
                     return "待提交";
                 } else if (d.status == 2) {
                     return "工程待审核";
                 } else if (d.status == 3){
                     return "工程驳回";
                 }  else if (d.status == 4) {
                    return "财务待审核";
                 } else if (d.status == 5){
                    return "财务驳回";
                 } else if (d.status == 6) {
                    return "待打款";
                 } else if (d.status == 7){
                    return "已打款";
                 }
             }},
            {field: 'remitAmount', sort: true, title: '打款金额'},
            {align: 'center', toolbar: '#tableBar', width: 100, title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BtyZxBaoxiaodanAudit.search = function () {
        var queryData = {};
        queryData.status = $('#status').val();
        queryData.shopName = $('#shopName').val();
        queryData.startDate = $('#startDate').val();
        queryData.endDate = $('#endDate').val();
        queryData.userId = $('#userId').val();

        table.reload(BtyZxBaoxiaodanAudit.tableId, {
            where: queryData, page: {curr: 1, limit: 20}
        });
    };

    /**
     * 跳转到添加页面
     */
    BtyZxBaoxiaodanAudit.jumpAddPage = function () {
        window.location.href = Feng.ctxPath + '/btyZxBaoxiaodanAudit/add'
    };

    /**
    * 跳转到编辑页面
    *
    * @param data 点击按钮时候的行数据
    */
    BtyZxBaoxiaodanAudit.jumpEditPage = function (data) {
        parent.layer.open({
            type: 2,
            title: '详情',
            shadeClose: true,
            shade: false,
            maxmin: true, //开启最大化最小化按钮
            area: ['40%', '60%'],
            content: Feng.ctxPath + '/btyZxBaoxiaodanAudit/edit?id=' + data.id,
            end: function () {
                BtyZxBaoxiaodanAudit.search();
            },
            cancel:function (index){
                BtyZxBaoxiaodanAudit.search();
            }
        });
        // func.open({
        //     title: '详情',
        //     content: Feng.ctxPath + '/btyZxBaoxiaodanAudit/edit?id=' + data.id,
        //     tableId: BtyZxBaoxiaodanAudit.tableId,
        //     width: '600px',
        // });
    };

    /**
     * 导出excel按钮
     */
    BtyZxBaoxiaodanAudit.exportExcel = function () {
        var checkRows = table.checkStatus(BtyZxBaoxiaodanAudit.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    BtyZxBaoxiaodanAudit.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/btyZxBaoxiaodanAudit/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(BtyZxBaoxiaodanAudit.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BtyZxBaoxiaodanAudit.tableId,
        url: Feng.ctxPath + '/btyZxBaoxiaodanAudit/list',
        page: true,
        limit: 20,
        height: "full-158",
        cellMinWidth: 100,
        cols: BtyZxBaoxiaodanAudit.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BtyZxBaoxiaodanAudit.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    BtyZxBaoxiaodanAudit.jumpAddPage();

    });

    // 导出excel
    $('#btnExp').click(function () {
        BtyZxBaoxiaodanAudit.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + BtyZxBaoxiaodanAudit.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            BtyZxBaoxiaodanAudit.jumpEditPage(data);
        } else if (layEvent === 'delete') {
            BtyZxBaoxiaodanAudit.onDeleteItem(data);
        }
    });
});
